package com.group.Controller;


import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.json.JSONObject;
import com.group.Tools.ChangePwd;
import com.group.Tools.PageInfo;
import com.group.Enity.User;
import com.group.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 获得验证码
     * @param req
     * @param resp
     */
    @RequestMapping("getCode.do")
    public void getCode(HttpServletRequest req, HttpServletResponse resp)
    {
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(50,25,4,5);
        String s = lineCaptcha.getCode();
        //req.getSession().setAttribute("Register_getCode", s);
        redisTemplate.opsForValue().set("registerCode",s,3, TimeUnit.MINUTES);

        try {
            lineCaptcha.write(resp.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 通过名称获得一个用户
     * @param req
     * @return
     */
    @ResponseBody
    @RequestMapping("findOne.do")
    public PageInfo findOne(HttpServletRequest req){
        String userName = req.getParameter("username");
        User user = new User();
        user.setUsername(userName);
        User mysqlUser = userService.getUser(user);
        PageInfo pageInfo = new PageInfo();

        if (mysqlUser == null) {
            pageInfo.setCode(200);
            pageInfo.setMsg("可以使用的用户名");
        }else{
            pageInfo.setCode(500);
            pageInfo.setMsg("用户名冲突！");
        }
        //pageInfo.setMsg("null");

        return pageInfo;
    }

    /**
     * 登录请求
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping("login.do")
    public String login(User user, HttpSession httpSession){

        String captcha = redisTemplate.opsForValue().get("registerCode");
        JSONObject jsonObject = new JSONObject();
        //如果验证码为空
        if (captcha == null)
        {

            jsonObject.accumulate("code",500);
            jsonObject.accumulate("msg","验证码已经过期了,从新点击图片吧");
        }else{

            //如果从用户那里收取的验证码不为空和相等
            //System.out.println("infor:" + user );
            if (captcha.equals(user.getCaptcha())){
                User mysqlUser = userService.getUser(user);
                if (mysqlUser != null) {
                    jsonObject.accumulate("code", 200);
                    jsonObject.accumulate("user", mysqlUser);
                    httpSession.setAttribute("loginUser",mysqlUser);
                }else
                {
                    jsonObject.accumulate("code",500);
                    jsonObject.accumulate("msg","账号或者密码错误");
                }
            }else{
                jsonObject.accumulate("code",500);
                jsonObject.accumulate("msg","验证码不正确");
            }
        }
        //System.out.println(user);
        //System.out.println(jsonObject);
        return jsonObject.toString();
    }

    /**
     * 注册请求
     */
    @RequestMapping("register.do")
    @ResponseBody
    public PageInfo register(User user)
    {
        //System.out.println("registerUser:" + user);
        String name = user.getUsername();
        String pwd = user.getPassword();
        userService.addUser(name,pwd);
        PageInfo pageInfo = new PageInfo();
        pageInfo.setCode(200);
        pageInfo.setMsg("注册成功用户");
        return pageInfo;
    }

    @ResponseBody
    @RequestMapping("changePassword.do")
    public PageInfo changePwd(ChangePwd changePwd,HttpSession httpSession){
        PageInfo pageInfo = new PageInfo();
        User u = (User) httpSession.getAttribute("loginUser");
        int id = u.getId();

        if(userService.checkPwd(id,changePwd.getOldPwd())) {
            userService.changePwd(id, changePwd.getNewPwd());
            pageInfo.setCode(200);
            pageInfo.setMsg("修改密码成功");
        }else{
            pageInfo.setCode(500);
            pageInfo.setMsg("旧密码错误");
        }
        return pageInfo;
    }

    @RequestMapping("logOut.do")
    public ModelAndView loginOut()
    {
        ModelAndView mv = new ModelAndView("redirect:/login.html");
        return mv;
    }


}
